<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD> 
  <TITLE> New Document </TITLE>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="/common/js/dhtmlxFull/dhtmlx.css" />
<script language='JavaScript' src='/common/js/jquery-1.11.0.js'></script>
<script language='JavaScript' src='/common/js/dhtmlxFull/dhtmlx.js'></script>
<script type="text/javascript" src="/common/js/jquery-1.11.0.js"></script>
<script type="text/javascript" src="/common/js/common.js"></script>
<script type="text/javascript" src="/common/js/serviceCall.js"></script>
<script type="text/javascript">
var DxGridFactory = function(){
	this._colList = new Array();
};
DxGridFactory.prototype.addCol = function(header, saveName, type, align, width){
	this._colList.push({header:header, saveName:saveName, type:type, align:align, width:width});
};
DxGridFactory.prototype.getHeader = function(){
	var rtnArr = new Array();
	$.each(this._colList, function(i, h){
		rtnArr.push(h.header);
	});
	return rtnArr;
	//return TermManager.getTerm(rtnArr).join(",");
};
DxGridFactory.prototype.getSaveNameArray = function(){
	var rtnArr = new Array();
	$.each(this._colList, function(i, h){
		rtnArr.push(h.saveName);
	});
	
	return rtnArr;
};
DxGridFactory.prototype.getType = function(){
	var rtnArr = new Array();
	$.each(this._colList, function(i, h){
		rtnArr.push(h.type);
	});
	
	return rtnArr.join(",");
};
DxGridFactory.prototype.getAlign = function(){
	var rtnArr = new Array();
	$.each(this._colList, function(i, h){
		rtnArr.push(h.align);
	});
	
	return rtnArr.join(",");
};
DxGridFactory.prototype.getWidth = function(){
	var rtnArr = new Array();
	$.each(this._colList, function(i, h){
		rtnArr.push(h.width);
	});
	
	return rtnArr.join(",");
};
DxGridFactory.prototype.getIndexBySaveName = function(saveNm){
	var rtnIdx = null;
	$.each(this.getSaveNameArray(), function(i, s){
		if(saveNm == s){
			rtnIdx = i;
			return false;
		}
	});
	
	return rtnIdx;
};

DxGridFactory.prototype.getColumnColor = function(){
	var rtnArr = new Array();
	$.each(this._colList, function(i, h){
		if(h.type == 'ro'){
			rtnArr.push("#E9EBED");	
		}else{
			rtnArr.push("#FFFFFF");
		}
	});
	
	return rtnArr.join(",");
};


var dxHeader = new DxGridFactory();
var dxGrid;
var gridData = null;
var serviceInfo = new serviceInfo();

dxHeader.addCol("서비스ID" , "serviceId", 	"ro", "left"	, "120"	);
dxHeader.addCol("클래스명" , "classNm", 	"ed", "left"	, "120"	);
dxHeader.addCol("메소드명" , "methodNm", 	"ed", "left"	, "120"	);
dxHeader.addCol("기능설명" , "comments", 	"ed", "left"	, "120"	);

$(function(){

	//set dhtmlx treegrid object
	dxGrid = new dhtmlXGridObject('dxGrid');
	dxGrid.setImagePath("/common/js/dhtmlxFull/imgs/");
	dxGrid.setHeader(dxHeader.getHeader());
	dxGrid.setInitWidths(dxHeader.getWidth());
	dxGrid.setColAlign(dxHeader.getAlign());
	dxGrid.setColTypes(dxHeader.getType());
	dxGrid.init();
	selectList();

	$("#save").click(function(){
		fn_saveData();
	});

	$("#addRow").click(function(){
		gridAddRow();
	});

	$("#delRow").click(function(){
		gridDelRow();
	});
});

function selectList(){
	var params = {};
	params.dbjob = "S";
	serviceInfo.process(function(data){
		$.each(data.serviceList, function(l, obj){
			var labelData = new Array();
			var dxHeaderInfo = dxHeader.getSaveNameArray();
			
			for(var k in dxHeaderInfo){
				if (obj[dxHeaderInfo[0]] == undefined) obj[dxHeaderInfo[0]] = "";
				labelData.push(gfn_nvl2(obj[dxHeaderInfo[k]],""));	
			}
			var rowId = obj.serviceId;
			dxGrid.addRow(rowId, labelData);
		});
	}, params);	
	dxGrid.clearChangedState();
}

//서비스추가
function gridAddRow(){
	var data = getGridDataToAdd();
	var lastIndex = data.length - 1;
	var lastServiceId = "";
	
	$.each(data, function(key, value){
		if(lastIndex == key){
			lastServiceId = value.serviceId;
		}
	});

	lastServiceId = lastServiceId.replace(/[^0-9]?/g,'');
	var idx = parseInt(lastServiceId);
	var tmpNm = "00000" + (idx + 1);
	
	var newServiceId = "SVC"+tmpNm.substr(tmpNm.length - 4, 4);
	var labelData = {};
	labelData.serviceId = newServiceId; 
	dxGrid.addRow(newServiceId, newServiceId); 
}

function gridDelRow(){
	if(confirm("삭제하시겠습니까?")){
		var data = {}; 
		data.serviceId = dxGrid.getSelectedId();
		dxGrid.deleteRow(data.serviceId);
		
		var params = {};
		params.data = data;
		params.dbjob = "D";
		serviceInfo.process(function(){
			
		}, params);
	}
}

//저장용
function getGridData(){
	var ids = dxGrid.getChangedRows();
	var changedRow = ids.split(","); 
	
	var saveNameList = dxHeader.getSaveNameArray();
	var rowListData = new Array();
	var boolValidation = true;
	var idx = 0;
	dxGrid.forEachRow(function(rId){
		var rowData = {};
		var focusIndex = "";
		
		rowData.serviceId = dxGrid.cells(rId, dxHeader.getIndexBySaveName("serviceId")).getValue();
		rowData.classNm = dxGrid.cells(rId, dxHeader.getIndexBySaveName("classNm")).getValue();
		rowData.methodNm = dxGrid.cells(rId, dxHeader.getIndexBySaveName("methodNm")).getValue();
		rowData.comments = dxGrid.cells(rId, dxHeader.getIndexBySaveName("comments")).getValue();
		
		$.each(rowData, function(i, v){
			if(v==''){
				focusIndex = i;
				boolValidation = false;
				return false;
			}
		});
		if($.inArray(rId, changedRow) > -1){
			rowListData.push(rowData);
		}

		if(boolValidation == false){
			dxGrid.selectCell(idx, focusIndex, true, false, true);
			return false;
		}
		idx++;
	});
	
	if(!boolValidation){
		alert('빈칸입력불가')
		return false;
	}
	console.log(rowListData)
	return rowListData;
}

//로우추가용 그리드데이터 읽어오기
function getGridDataToAdd(){
	
	var saveNameList = dxHeader.getSaveNameArray();
	var rowListData = new Array();
	var boolValidation = true;

	dxGrid.forEachRow(function(rId){
		var rowData = {};

		rowData.serviceId = dxGrid.cells(rId, dxHeader.getIndexBySaveName("serviceId")).getValue();
		rowData.classNm = dxGrid.cells(rId, dxHeader.getIndexBySaveName("classNm")).getValue();
		rowData.methodNm = dxGrid.cells(rId, dxHeader.getIndexBySaveName("methodNm")).getValue();
		rowData.comments = dxGrid.cells(rId, dxHeader.getIndexBySaveName("comments")).getValue();
		
		rowListData.push(rowData);
	});
	
	if(!boolValidation){
		return false;
	}
	return rowListData;
}


function fn_saveData(){
	var data = getGridData();
	if(data.length <= 0 || data == false){
		return;
	}
	var params = {};
	params.data = data;
	params.dbjob = "I";
	serviceInfo.process(function(){
		dxGrid.clearChangedState();
		alert('저장완료');
	}, params);
	
}


</script>
<body>

<div class="cntsArea mb15">
		<div class="grid" id="dxGrid" style="width:100%;height:280px"></div>
</div>
<button id="save">저장</button>
<button id="addRow">서비스추가</button>
<button id="delRow">삭제</button>
</body>
</HTML>









